using System;
using System.Data;
using System.Collections;
using System.Windows.Forms;

namespace WeifenLuo.WinFormsUI.Base
{
	public struct AppSecurityAttribute 
	{
		public string ms_formName;
		public string ms_Module;
		public bool mb_read;		// default value is false
		public bool mb_add;			// default value is false
		public bool mb_delete;		// default value is false
		public bool mb_modify;		// default value is false
		public bool mb_print;		// default value is false
		public bool mb_post;		// default value is false
		public bool mb_closeopen;	// default value is false

		public	AppSecurityAttribute AND(AppSecurityAttribute access)
		{
			AppSecurityAttribute	appSecurityAttribute = new AppSecurityAttribute();
			
			appSecurityAttribute.ms_formName	= ms_formName;
			appSecurityAttribute.ms_Module		= ms_Module;
			appSecurityAttribute.mb_read		= mb_read		& access.mb_read;
			appSecurityAttribute.mb_add			= mb_add		& access.mb_add;
			appSecurityAttribute.mb_delete		= mb_delete		& access.mb_delete;
			appSecurityAttribute.mb_modify		= mb_modify		& access.mb_modify;
			appSecurityAttribute.mb_print		= mb_print		& access.mb_print;
			appSecurityAttribute.mb_post		= mb_post		& access.mb_post;
			appSecurityAttribute.mb_closeopen	= mb_closeopen	& access.mb_closeopen;

			return appSecurityAttribute;
		}

		public	AppSecurityAttribute OR(AppSecurityAttribute access)
		{
			AppSecurityAttribute	appSecurityAttribute = new AppSecurityAttribute();
			
			appSecurityAttribute.ms_formName	= ms_formName;
			appSecurityAttribute.ms_Module		= ms_Module;
			appSecurityAttribute.mb_read		= mb_read		| access.mb_read;
			appSecurityAttribute.mb_add			= mb_add		| access.mb_add;
			appSecurityAttribute.mb_delete		= mb_delete		| access.mb_delete;
			appSecurityAttribute.mb_modify		= mb_modify		| access.mb_modify;
			appSecurityAttribute.mb_print		= mb_print		| access.mb_print;
			appSecurityAttribute.mb_post		= mb_post		| access.mb_post;
			appSecurityAttribute.mb_closeopen	= mb_closeopen	| access.mb_closeopen;

			return appSecurityAttribute;
		}
	}

	/// <summary>
	/// Summary description for AppSecurity.
	/// </summary>
	public class AppSecurity
	{
		private DataSet	mDS_AccessRight;
		public AppSecurity()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public bool GetRightSet(DataSet aDS_AccessRight)
		{
			if (aDS_AccessRight == null)	return false;
			mDS_AccessRight = aDS_AccessRight;
			return true;
		}

		// Get access from the DataSet then apply to the form.
		public  AppSecurityAttribute GetAccess(string as_ObjectName)
		{
			AppSecurityAttribute lstr_SecurityAttribute = new AppSecurityAttribute();

			// Kiem tra xem user thuoc nhung group nao?
			// Ung voi moi group, lay tat ca cac form ma group do co the thao tac duoc
			// Kiem tra xem form nay co mo duoc tu nhieu group khong?
			// Neu duoc mo tu nhieu group thi OR quyen cac groups cho form do'
	
			// Set enabled or disabled menu for each user according to the Group's right

			// Only select rows that ID_Object = as_ObjectName 
			string ls_Expr = "ID_Object = '" + as_ObjectName + "'";
			
			DataTable ldataTable = mDS_AccessRight.Tables[0];
			DataRow[] laDataRows = ldataTable.Select(ls_Expr);

			if (laDataRows != null)
			{
				DataRow dr;
				for (int li_row = 0; li_row < laDataRows.Length; li_row++)
				{
					if (li_row == 0)
					{
						dr = laDataRows[0];
						lstr_SecurityAttribute.mb_read		= Convert.ToBoolean(dr["Read_Access"]);
						lstr_SecurityAttribute.mb_add		= Convert.ToBoolean(dr["Add_Access"]);
						lstr_SecurityAttribute.mb_modify	= Convert.ToBoolean(dr["Modify_Access"]);
						lstr_SecurityAttribute.mb_delete	= Convert.ToBoolean(dr["Delete_Access"]);
						lstr_SecurityAttribute.mb_print		= Convert.ToBoolean(dr["Print_Access"]);
						lstr_SecurityAttribute.mb_post		= Convert.ToBoolean(dr["Post_Access"]);
						lstr_SecurityAttribute.mb_closeopen	= Convert.ToBoolean(dr["CloseOpenDoc_Access"]);
					}
					else
					{
						dr = laDataRows[li_row];
						lstr_SecurityAttribute.mb_read		|= Convert.ToBoolean(dr["Read_Access"]);
						lstr_SecurityAttribute.mb_add		|= Convert.ToBoolean(dr["Add_Access"]);
						lstr_SecurityAttribute.mb_modify	|= Convert.ToBoolean(dr["Modify_Access"]);
						lstr_SecurityAttribute.mb_delete	|= Convert.ToBoolean(dr["Delete_Access"]);
						lstr_SecurityAttribute.mb_print		|= Convert.ToBoolean(dr["Print_Access"]);
						lstr_SecurityAttribute.mb_post		|= Convert.ToBoolean(dr["Post_Access"]);
						lstr_SecurityAttribute.mb_closeopen	|= Convert.ToBoolean(dr["CloseOpenDoc_Access"]);
					}
				}			
			}
			// This form is not exist in the Database --> The form cannot be opened
			return lstr_SecurityAttribute;
		}

		#region No need now
		public  AppSecurityAttribute GetAccess(string as_ObjectName, string as_ModuleID)
		{
			AppSecurityAttribute lstr_SecurityAttribute = new AppSecurityAttribute();

			// Kiem tra xem user thuoc nhung group nao?
			// Ung voi moi group, lay tat ca cac form trong Module tuong ung ma group do co the thao tac duoc
			// Kiem tra xem form nay co mo duoc tu nhieu group khong?
			// Neu duoc mo tu nhieu group thi OR quyen cac groups cho form do'
	
			// Set enabled or disabled menu for each user according to the Group's right

			// Only select rows that ID_Object = as_ObjectName and ID_Module = as_ModuleID
			string ls_Expr =	"ID_Object = '" + as_ObjectName + "' and " + 
								"ID_Module = '" + as_ModuleID + "'";
			DataTable ldataTable = mDS_AccessRight.Tables[0];
			DataRow[] laDataRows = ldataTable.Select(ls_Expr);
			// laDataRows always only has 0 or 1 row
			if (laDataRows != null)
			{	// So we dont need to make a loop like this.
				for (int li_row = 0; li_row < laDataRows.Length; li_row++)
				{
					if (li_row == 0)
					{
						lstr_SecurityAttribute.mb_read		= Convert.ToBoolean(laDataRows[0]["Read_Access"]);
						lstr_SecurityAttribute.mb_add		= Convert.ToBoolean(laDataRows[0]["Add_Access"]);
						lstr_SecurityAttribute.mb_modify	= Convert.ToBoolean(laDataRows[0]["Modify_Access"]);
						lstr_SecurityAttribute.mb_delete	= Convert.ToBoolean(laDataRows[0]["Delete_Access"]);
						lstr_SecurityAttribute.mb_print		= Convert.ToBoolean(laDataRows[0]["Print_Access"]);
						lstr_SecurityAttribute.mb_post		= Convert.ToBoolean(laDataRows[0]["Post_Access"]);
					}
					else
					{
						lstr_SecurityAttribute.mb_read		|= Convert.ToBoolean(laDataRows[li_row]["Read_Access"]);
						lstr_SecurityAttribute.mb_add		|= Convert.ToBoolean(laDataRows[li_row]["Add_Access"]);
						lstr_SecurityAttribute.mb_modify	|= Convert.ToBoolean(laDataRows[li_row]["Modify_Access"]);
						lstr_SecurityAttribute.mb_delete	|= Convert.ToBoolean(laDataRows[li_row]["Delete_Access"]);
						lstr_SecurityAttribute.mb_print		|= Convert.ToBoolean(laDataRows[li_row]["Print_Access"]);
						lstr_SecurityAttribute.mb_post		|= Convert.ToBoolean(laDataRows[li_row]["Post_Access"]);
					}
				}
			}
			// This form is not exist in the Database --> The form cannot be opened
			return lstr_SecurityAttribute;
		}
		#endregion
		// Check the user in ADMIN group or not
		public bool IsInAdminGroup(string as_userName)
		{
			try
			{
				string ls_Expr = "ID_Group = 'ADMIN' And ID_User = '" + as_userName + "'";
				DataTable ldataTable = mDS_AccessRight.Tables[0];

				DataRow[] laDataRows = ldataTable.Select(ls_Expr);

				if (laDataRows != null && laDataRows.Length > 0)	return true;
				return false;
			}
			catch(Exception ex)
			{
				Ctrl_LOG.Print("Base Security", "In the method IsInAdminGroup.", "Error message: " + ex.Message);
				return false;
			}
		}
	}
}
